//生成id
function l_id(){
    let today = new Date().getTime()
    return(today)
}	

function gradeChange(obj) {
    let par = $(obj).closest(".new")[0];

    let j = par.querySelector(".time_count") //间隔时间
    let k = par.querySelector(".time_last") //剩余时间

    k.value = j.value + "分钟"
}

function progress_count(i) {

    t_temp = (i["t"] - new Date().getTime()) / 1000
    let k_temp = Math.floor(t_temp / (60)) + "分钟" //剩余分钟数
    cur_task = 100 * t_temp / (60 * i["j"].value) //进度条显示数值
    return ([cur_task, k_temp])

}
//加载读条数据
function e_load(elm){
	let temp = {}
	temp["j"] = elm.querySelector(".time_count") //间隔时间
    temp["k"] = elm.querySelector(".time_last") //剩余时间
    temp["w"] = elm.querySelector(".xuexi_neirong") //学习内容
    temp["d"] = elm.querySelector(".progress1") //进度条
    temp["m"] = elm.querySelector(".strat_run") //开始按钮
    return(temp)
}
//开始计时按钮
function start_run(obj) {
    let par = $(obj).closest(".new")[0]
    let temp = e_load(par)
    temp['d'].parentElement.style.background=""
    temp["t"] = temp["j"].value * 60 * 1000 + new Date().getTime() //开始时间
    let l = par.id
	l = parseInt(l)

    //temp["m"].disabled = true //按钮不可用
    //temp["j"].disabled = true

    counter_list[l] = temp  //整合信息提交给实时更新的counter_list
    
    //向数据库提交信息
    let data_temp = {
        "id": l,
        "j": temp.j.value,
        "w": temp.w.value,
        "t": temp.t,
    }
    
    addData(db, storeName, data_temp)

}

//根据情况添加时间标签
function judge_time_tag(i,m){
    m_b = parseInt(m["k"].value)
    let b = m_b<review_time[0]&&m_b>=0  //判断时间是否过期
    let tag =document.getElementById(i)
    if(b){

        
        if(!tag.querySelector(".tag_time")){
            let par = document.getElementById(i)
            par = par.querySelector(".add_tag")

            let temp = tag_cell.cloneNode(true)
            temp.querySelector(".tag_input").value = review_time[1]
            temp.querySelector(".tag_input").classList.add("tag_time")

            par.parentNode.insertBefore(temp, par)
            tag_change(m["d"])
        }
        }else if(tag.querySelector(".tag_time")){
            let t =tag.querySelector(".tag_time")
            if (m_b>review_time[0]){
                del_tag(t)
            }
        }

}

function keep_run() //一直在跑的程序
{
    for (var i in counter_list) {
        let m = counter_list[i]
        let temp = []
        temp = progress_count(m)
        m["d"].value = temp[0]
        m["k"].value = temp[1]

        setTimeout(judge_time_tag(i,m),1000)

        if (m["d"].value == 0) {
            m["k"].value = "已经间隔" + m["j"].value + "分钟"
            delete counter_list[i]
            //m["m"].disabled = false //按钮可用
            //m["j"].disabled = false
            m["j"].selectedIndex = m["j"].selectedIndex + 1
            m['d'].parentElement.style.background="#c92a2a"
        }

    }


}

//增加新项_核心函数
function add_data_in(l,j="",w="",t="") {
    let temp = tbody_d.cloneNode(true)
    temp.id = l
    l_count = l_count+1
    temp.querySelector(".list_count").innerHTML = l_count
	temp.querySelector(".time_count").value = j
	temp.querySelector(".xuexi_neirong").value = w
    
    
    let bg_color = ["#F7FAFD","#ccebc5"]
    temp.style.backgroundColor=bg_color[(l_count%2)]
	
    let da = document.getElementById("data");
    temp = da.appendChild(temp)
	
	if (t){
		let list = e_load(temp)
		list['t']=t
		counter_list[l] = list
	}
}

//增加新项_外壳
function add_data() {
	let l = l_id()
    add_data_in(l,j="5",w="",t="")
	
}

//删除
function del_this(obj){
	let par = $(obj).closest(".new")[0]
    let l = par.id
    var r = confirm("您确定删除本项吗？")
	if (r == true) {
	    del_one(db,storeName,l)
        del_one(db,store_tag,l)
    }
    location.reload(false)
}

//全部删除
function del_data(){
    var r = confirm("您确定清空数据集吗？")
    if (r == true) {
        del_all(db,storeName)
        del_all(db,store_tag)
        location.reload(false)

        let data_temp = [{"re_list":0,"search_tab":[]},{"re_list":1,"result_list":[]}]                

        addData(db, search_tag, data_temp[0])
        addData(db, search_tag, data_temp[1])
    } 	
}